package org.checkboo.hopefullyengine.loaders;

import org.checkboo.math.Vector2f;
import org.checkboo.math.Vector3f;
import org.xml.sax.Attributes;
import org.xml.sax.SAXException;
import org.xml.sax.helpers.DefaultHandler;

public class OgreMeshHandler extends DefaultHandler{
	
	private Vector3f[] vertices;
	private int[] indices;
	private Vector2f[] texCoords;
	private Vector3f[] normals;
	private int[] boneAssings;
	private int v=-1,i=-1,t=-1,n=-1,b=-1;
	private boolean inVertex = false;
	private String skeletonXML;

	@Override
	public void endElement(String uri, String localName, String qName)
			throws SAXException {
		if(qName.equalsIgnoreCase("vertex")){
			this.inVertex = false;
		}
		super.endElement(uri, localName, qName);
	}

	@Override
	public void startElement(String uri, String localName, String qName,
			Attributes attributes) throws SAXException {
		if(qName.equalsIgnoreCase("faces")){
			this.indices = new int[Integer.parseInt(attributes.getValue("count"))*3];
		}
		else if(qName.equalsIgnoreCase("face")){
			this.indices[++i] = Integer.parseInt(attributes.getValue("v1"));
			this.indices[++i] = Integer.parseInt(attributes.getValue("v2"));
			this.indices[++i] = Integer.parseInt(attributes.getValue("v3"));
		}
		else if(qName.equalsIgnoreCase("geometry")){
			int count = Integer.parseInt(attributes.getValue("vertexcount"));
			this.vertices = new Vector3f[count];
			this.normals = new Vector3f[count];
			this.texCoords = new Vector2f[count];
			this.boneAssings = new int[count];
		}
		else if(qName.equalsIgnoreCase("vertex")){
			this.inVertex = true;
		}
		else if(qName.equalsIgnoreCase("position")){
			if(inVertex){
				this.vertices[++v] = new Vector3f(Float.parseFloat(attributes.getValue("x")), 
													Float.parseFloat(attributes.getValue("y")), 
													Float.parseFloat(attributes.getValue("z")));
			}
		}
		else if(qName.equalsIgnoreCase("normal")){
			if(inVertex){
				this.normals[++n] = new Vector3f(Float.parseFloat(attributes.getValue("x")), 
													Float.parseFloat(attributes.getValue("y")), 
													Float.parseFloat(attributes.getValue("z")));
			}
		}
		else if(qName.equalsIgnoreCase("texcoord")){
			if(inVertex){
				this.texCoords[++t] = new Vector2f(Float.parseFloat(attributes.getValue("u")), 
													Float.parseFloat(attributes.getValue("v")));
			}
		}
		else if(qName.equalsIgnoreCase("vertexboneassignment")){
			this.boneAssings[Integer.parseInt(attributes.getValue("vertexindex"))] = Integer.parseInt(attributes.getValue("boneindex"));
			
		}
		else if(qName.equalsIgnoreCase("skeletonlink"))
			this.skeletonXML = attributes.getValue("name");
		super.startElement(uri, localName, qName, attributes);
	}

	public Vector3f[] getVertices() {
		return vertices;
	}

	public void setVertices(Vector3f[] vertices) {
		this.vertices = vertices;
	}

	public int[] getIndices() {
		return indices;
	}

	public void setIndices(int[] indices) {
		this.indices = indices;
	}

	public Vector2f[] getTexCoords() {
		return texCoords;
	}

	public void setTexCoords(Vector2f[] texCoords) {
		this.texCoords = texCoords;
	}

	public Vector3f[] getNormals() {
		return normals;
	}

	public void setNormals(Vector3f[] normals) {
		this.normals = normals;
	}

	public int[] getBoneAssings() {
		return boneAssings;
	}

	public void setBoneAssings(int[] boneAssings) {
		this.boneAssings = boneAssings;
	}

	public String getSkeletonXML() {
		return skeletonXML;
	}

	public void setSkeletonXML(String skeletonXML) {
		this.skeletonXML = skeletonXML;
	}
}
